package com.cn.kehong.utils;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class GenerateUUID {

    @Autowired
    RedisTemplate redisTemplate;

    private Object lock = new Object();

    public  String getYearStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
        String dateStr = simpleDateFormat.format(new Date());
        return dateStr;
    }

    public  String getYearFourStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String dateStr = simpleDateFormat.format(new Date());
        return dateStr;
    }
    public  String getYearTwoStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy");
        String dateStr = simpleDateFormat.format(new Date());
        return dateStr;
    }

    public  String getYearMoth(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMM");
        String dateStr = simpleDateFormat.format(new Date());
        return dateStr;
    }

    public int getTempNumberFlag(String  flag){
        int num = -1;
        if(StringUtils.isEmpty(flag)){
            flag = "commonFlag";
        }
        Object o = redisTemplate.opsForValue().get(flag);
        if(o == null){
            return 1;
        }
        num = Integer.parseInt(o.toString());
        num +=1;
        return num;
    }

    public int getNumberFlag(String flag){
        int num = -1;
        if(StringUtils.isEmpty(flag)){
            flag = "commonFlag";
        }

        synchronized (lock){
            Object o = redisTemplate.opsForValue().get(flag);
            if(o == null){
                redisTemplate.opsForValue().set(flag,1+"");
                return 1;
            }
            num = Integer.parseInt(o.toString());
            num +=1;
            redisTemplate.opsForValue().set(flag,num+"");
        }
        return num;
    }

    public String getTwoNumStr(int num){
        String str = "";
        if(num < 10){
            str = "0";
        }
        str+=num;
        return str;
    }


    public String getThreeNumStr(int num){
        String str = "";
        if(num < 10){
            str = "00";
        }else if(num < 100){
            str = "0";
        }
        str+=num;
        return str;
    }



    public String getFourNumStr(int num){
        String str = "";
        if(num < 10){
            str = "000";
        }else if(num < 100){
            str = "00";
        }else if(num < 1000){
            str = "0";
        }
        str+=num;
        return str;
    }

    public String getFiveNumStr(int num){
        String str = "";
        if(num < 10){
            str = "0000";
        }else if(num < 100){
            str = "000";
        }else if(num < 1000){
            str = "00";
        }else if(num < 10000){
            str = "0";
        }
        str+=num;
        return str;
    }

    public String getSixNumStr(int num){
        String str = "";
        if(num < 10){
            str = "00000";
        }else if(num < 100){
            str = "0000";
        }else if(num < 1000){
            str = "000";
        }else if(num < 10000){
            str = "00";
        }else if(num < 100000){
            str = "0";
        }
        str+=num;
        return str;
    }



}
